package com.it.myh.query.probability;

import com.it.myh.read.LocationAndTime;
import com.it.myh.read.ReadPltFile;

import java.io.*;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: queryProbability    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2023/6/19 11:26   // 时间
 * @Version: 1.0     // 版本
 */

public class queryProbability {

    private static final String DATA_SET_PATH = "C:\\Users\\IoV321\\Desktop\\test";

    private static final String PROCESSED_DATA_SET_PATH = "C:\\Users\\IoV321\\Desktop\\result.txt";

    public static void main(String[] args) {
//        System.out.println("开始处理数据，开始时间为：" + LocalDateTime.now());  //开始处理数据，开始时间为：2023-06-24T20:46:12.640
//        calculatePro();
        List<LocationAndTime> locationAndTimes = deserializeLocationAndTime(PROCESSED_DATA_SET_PATH);
        List<LocationAndTime> sortPro = sortPro(locationAndTimes);
        for (int i = 0; i < 100; i++) {
            System.out.println(sortPro.get(i));
        }
//        System.out.println("数据处理完毕，现在时间为：" + LocalDateTime.now());

    }

    //统计数据集中各个位置出现的次数
    private static List<LocationAndTime> locationCount(){
        List<LocationAndTime> locationAndTimes = ReadPltFile.readPltFiles(DATA_SET_PATH);
        for (int i = 0; i < locationAndTimes.size(); i++) {
            if (i % 10000 == 0){
                System.out.println("总数据为：" + locationAndTimes.size() + "，正在处理第" + i +"条数据，现在时间为：" + LocalDateTime.now());
            }
            TimeZone whichZone = TimeZone.whichZone(locationAndTimes.get(i).getTime());
            whichZone.setNum(whichZone.getNum() + 1);
            for (int j = i + 1; j < locationAndTimes.size(); j++) {
                boolean sameLocation = LocationAndTime.isSameLocation(locationAndTimes.get(i), locationAndTimes.get(j));
                boolean belongZone = TimeZone.isBelongZone(locationAndTimes.get(j).getTime(), whichZone);
                if (sameLocation && belongZone){
                    whichZone.setNum(whichZone.getNum() + 1);
                    locationAndTimes.get(i).setNum(locationAndTimes.get(i).getNum() + 1);
                    locationAndTimes.remove(locationAndTimes.get(j));
                }
            }
        }
        System.out.println("各个位置出现的次数已经统计完毕，现在时间为：" + LocalDateTime.now());
        return locationAndTimes;
    }
    // 将对象集合序列化并写入硬盘
    private static void serializeLocationAndTime(List<LocationAndTime> locationAndTimes, String fileName) {
        try {
            FileOutputStream fileOut = new FileOutputStream(fileName);
            ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(locationAndTimes);
            objectOut.close();
            fileOut.close();
            System.out.println("对象集合已序列化并写入硬盘。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 从硬盘中读取对象集合并进行反序列化
    public static List<LocationAndTime> deserializeLocationAndTime(String fileName) {
        List<LocationAndTime> object = null;
        try {
            FileInputStream fileIn = new FileInputStream(fileName);
            ObjectInputStream objectIn = new ObjectInputStream(fileIn);
            object = (List<LocationAndTime>) objectIn.readObject();
            objectIn.close();
            fileIn.close();
            System.out.println("对象集合已从硬盘中反序列化。");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return object;
    }
    //计算各个位置的访问概率
    public static void calculatePro(){
        List<LocationAndTime> locationAndTimes = locationCount();
        System.out.println("计算各个位置的访问概率，现在时间为：" + LocalDateTime.now());
        for (LocationAndTime locationAndTime : locationAndTimes) {
            locationAndTime.setProbability((double)locationAndTime.getNum() / locationAndTime.getTimeZone().getNum());
        }
        System.out.println("访问概率计算完毕，现在将数据序列化到txt文件当中，现在时间为：" + LocalDateTime.now());
        serializeLocationAndTime(locationAndTimes,PROCESSED_DATA_SET_PATH);
        System.out.println("序列化完毕，现在正在进行反序列化读取，现在时间为：" + LocalDateTime.now());
        List<LocationAndTime> result = deserializeLocationAndTime(PROCESSED_DATA_SET_PATH);
        for (LocationAndTime locationAndTime : result) {
            System.out.println(locationAndTime);
        }
    }
    //根据查询概率来将此集合对象进行排序
    public static List<LocationAndTime> sortPro(List<LocationAndTime> locationAndTimes){
        Comparator<LocationAndTime> proComparator = Comparator.comparingDouble(LocationAndTime::getProbability);
        Collections.sort(locationAndTimes,Collections.reverseOrder(proComparator));
        return locationAndTimes;
    }



}
